package com.dmlll.controller;

import com.alibaba.fastjson.JSONObject;
import com.dmlll.VO.AccountVO;
import com.dmlll.VO.ParticularsVO;
import com.dmlll.VO.RegisterMessageVO;
import com.dmlll.pojo.PopulaceUser;
import com.dmlll.service.PopulaceUserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class PopulaceUserController {
    @Autowired
    private PopulaceUserService populaceUserService;

    @RequestMapping("/insertUser")
    @ResponseBody
    public PopulaceUser insertUser(PopulaceUser populaceUser){
        populaceUser.setAuditRegister(1);
        populaceUserService.insertUser(populaceUser);
        return populaceUser;
    }

    @RequestMapping("/login")
    @ResponseBody
    public Integer  login(HttpSession session,PopulaceUser populaceUser){
        PopulaceUser login = populaceUserService.login(populaceUser.getUserAccount());
        if(login != null) {
            if (login.getAuditRegister()==2) {
                if (login.getUserPasswrod().equals(populaceUser.getUserPasswrod())) {
                    session.setAttribute("user", login);
                    return login.getId();//
                }
                return 0;//密码不对
            }
            return -1;//没有权限
        }
        return -2;//没有账号
    }

    @RequestMapping("/selectAll")
    @ResponseBody
    public PageInfo<AccountVO>selectAll(HttpSession session,
            @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "6")Integer pageSize,
            @RequestParam(value = "userPhone",required = false)String userPhone
    ){
        Integer grade = ((PopulaceUser)session.getAttribute("user")).getAccountGrade()-1;
        PageInfo<AccountVO> list = populaceUserService.selectAll(userPhone,pageNum,pageSize,grade);
        return list;
    }

    //注册审核
    @RequestMapping("/selectRegister")
    @ResponseBody
    public PageInfo<RegisterMessageVO>selectRegister(
            @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "6")Integer pageSize,
            @RequestParam(value = "userPhone",required = false)String userPhone,
            @RequestParam(value = "accountGrade",defaultValue = "")Integer accountGrade,
            HttpSession session
    ){
        PopulaceUser user = (PopulaceUser)session.getAttribute("user");
        accountGrade = user.getAccountGrade();
        PageInfo<RegisterMessageVO> list = populaceUserService.selectRegister(userPhone,pageNum,pageSize,accountGrade);
        return list;
    }

    //查看详情
    @RequestMapping("/selectExamine")
    @ResponseBody
    public ParticularsVO selectExamine(Integer id){
        return populaceUserService.selectExamine(id);
    }

    //修改审核状态（通过）
    @RequestMapping("/updateAudit")
    @ResponseBody
    public Integer updateAudit(@RequestParam("idList[]") Integer[] idList){
        return populaceUserService.updateAudit(idList);
    }

    //修改审核状态(不通过)
    @RequestMapping("/updateAuditNO")
    @ResponseBody
    public Integer updateAuditNO(@RequestParam("idList[]") Integer[] idList,@RequestParam("notPassCause")String notPassCause){
        return populaceUserService.updateAuditNO(idList,notPassCause);
    }

    //查询所有的账号（避免重复注册）
    @RequestMapping("/selectUserAccount")
    @ResponseBody
    public List<PopulaceUser>selectUserAccount(){
        return populaceUserService.selectUserAccount();
    }

}
